/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.race.service;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.lczyfz.istep.modules.race.dao.RaceScheduleTeamSubjectDao;
import com.lczyfz.istep.modules.race.entity.RaceCtfSubject;
import com.lczyfz.istep.modules.race.entity.RaceRepertory;
import com.lczyfz.istep.modules.race.web.RaceLoginController;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSubjectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.race.entity.RaceSchedule;
import com.lczyfz.istep.modules.race.dao.RaceScheduleDao;

/**
 * 赛程管理Service
 * @author CY
 * @version 2017-04-26
 */
@Service
@Transactional(readOnly = true)
public class RaceScheduleService extends CrudService<RaceScheduleDao, RaceSchedule> {
	@Autowired
	RaceScheduleDao raceScheduleDao;

	@Autowired
	RaceScheduleTeamSubjectDao raceScheduleTeamSubjectDao;

	public RaceSchedule get(String id) {
		return super.get(id);
	}
	
	public List<RaceSchedule> findList(RaceSchedule raceSchedule) {
		return super.findList(raceSchedule);
	}
	
	public Page<RaceSchedule> findPage(Page<RaceSchedule> page, RaceSchedule raceSchedule) {
		return super.findPage(page, raceSchedule);
	}
	
	@Transactional(readOnly = false)
	public void save(RaceSchedule raceSchedule) {
		super.save(raceSchedule);
	}
	
	@Transactional(readOnly = false)
	public void delete(RaceSchedule raceSchedule) {
		super.delete(raceSchedule);
	}

    public 	RaceSchedule teamDetails(String id){
		return  raceScheduleDao.teamDetails(id);
	}

	public   List<RaceRepertory> findRepertory(){
		return  raceScheduleDao.findRepertory();
	}


	public List<RaceCtfSubject> findRaceSubject(String repertoryId){
		return raceScheduleDao.findRaceSubject(repertoryId);
	}

	public List<RaceCtfSubject> findFuzzyQuery(RaceCtfSubject raceCtfSubject){
		return raceScheduleDao.findFuzzyQuery(raceCtfSubject);
	}

	public ResourceCtfSubjectType findCtfSubjectType(String id){
		return  raceScheduleDao.findCtfSubjectType(id);
	}

	public RaceSchedule scheduleStatus(){
		return raceScheduleDao.scheduleStatus();
	}


    public static  int timeValue = 0;
	public static  Timer t;
	public  void time(final int time) {
	t = new Timer();
		t.schedule(new TimerTask() {
			private int coun = time;
			public void run() {
				this.coun--;
				timeValue = this.coun;
				if (coun <= 0){
					raceScheduleDao.finish();
					t.cancel();
					//倒计时结束，重置第一到第五名提交的计数器
					RaceLoginController.number = 0;
					timeValue = 0;
				}
			}
		}, 0, 1000);
	}




//   public  RaceSchedule raceSchedule;
//	public  int timeid;
	//登录模块的计时
   public static  int timeValue1 = 0;
	public static  int ti = 0;
	public static  Timer t1;
	public  void time1(final int time ,final RaceSchedule raceSchedule, final int timeid) {
		ti = 1;
		t1 = new Timer();
		t1.schedule(new TimerTask() {
			private int coun = time;
			RaceSchedule raceSchedule1 = raceSchedule;
			private int timeid1 = timeid;
			public void run() {
				this.coun--;
				timeValue1 = this.coun;
				if (coun <= 0){
					raceScheduleDao.finish();
					t1.cancel();
					timeValue1 = 0;
					ti = 0;

					if ((raceSchedule1 != null)){
						save(raceSchedule1);
					}else {

					}

					time(timeid1);
				}
			}
		}, 0, 1000);
	}

	@Transactional(readOnly = false)

	public void cleanData() {
		raceScheduleDao.cleanData();
	}
}

